package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.info.MenusInfo;
import com.woniuxy.controller.info.Result;
import com.woniuxy.model.Account;
import com.woniuxy.model.Menus;
import com.woniuxy.dao.MenusDao;
import com.woniuxy.service.MenusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.util.WoniuCopyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-15
 */
@Service
public class MenusServiceImpl extends ServiceImpl<MenusDao, Menus> implements MenusService {
    @Resource
    protected MenusDao menusDao;

    public Result getMenusbyId(Integer id){
        List<Menus> menusByUserId = menusDao.findMenusByUserId(id);
        List<MenusInfo> menusInfos = WoniuCopyUtil.copyMany(menusByUserId, MenusInfo.class);
        Map<Integer,MenusInfo> mp= new HashMap<>();
        ArrayList<MenusInfo> arlist = new ArrayList<>();
//        for (MenusInfo menusInfo : menusInfos) {
//            if (menusInfo.getMenusParentid()==0) {
//                arlist.add(menusInfo);
//                for (MenusInfo menusInfo2 : menusInfos) {
//                    if (menusInfo2.getMenusParentid()==menusInfo.getMenusId()) {
//                        if(menusInfo.getChildren()==null) menusInfo.setChildren(new ArrayList<MenusInfo>());
//                        menusInfo.getChildren().add(menusInfo2);
//                    }
//                }
//            }
//        }
        for (MenusInfo menusInfo : menusInfos) {
            mp.put(menusInfo.getMenusId(),menusInfo);
        }
        for (MenusInfo menusInfo : menusInfos) {
            if(menusInfo.getMenusParentid()==0){
                arlist.add(menusInfo);
            }else {
                if(mp.get(menusInfo.getMenusParentid()).getChildren()==null)mp.get(menusInfo.getMenusParentid()).setChildren(new ArrayList<MenusInfo>());
                mp.get(menusInfo.getMenusParentid()).getChildren().add(menusInfo);
            }
        }
        return Result.getInstanceList(MenusInfo.class).setData(arlist);
    }

    @Override
    public Result getAllMenusWithIndentify() {
        List<Menus> menus = menusDao.selectList(null);
        List<MenusInfo> menusInfos = WoniuCopyUtil.copyMany(menus, MenusInfo.class);
//        List<Integer> list=menusDao.seleMenusIdbyRuleId(ruleId);
        Map<Integer,MenusInfo> mp= new HashMap<>();
        ArrayList<MenusInfo> arlist = new ArrayList<>();
        for (MenusInfo menusInfo : menusInfos) {
            mp.put(menusInfo.getMenusId(),menusInfo);
        }
        for (MenusInfo menusInfo : menusInfos) {
            if(menusInfo.getMenusParentid()==0){
                arlist.add(menusInfo);
            }else {
                if(mp.get(menusInfo.getMenusParentid()).getChildren()==null)mp.get(menusInfo.getMenusParentid()).setChildren(new ArrayList<MenusInfo>());
                mp.get(menusInfo.getMenusParentid()).getChildren().add(menusInfo);
            }
        }
//        System.out.println(arlist);
        return Result.getInstanceList(MenusInfo.class).setData(arlist);
    }

    @Override
    //获取菜单id
    public Result getDefaultKey(Integer ruleId) {
        List<Integer> list=menusDao.seleMenusIdbyRuleId(ruleId);
        QueryWrapper<Menus> queryWrapper = new QueryWrapper<>();

        queryWrapper.in("menus_id",list).eq("menus_type",1);
        List<Menus> menus = menusDao.selectList(queryWrapper);
        List<Integer> collect = menus.stream().map(rm -> rm.getMenusId()).collect(Collectors.toList());

        return Result.getInstanceList(Integer.class).setData(collect);
    }

}
